<?php

namespace App\Http\Validate\OpenShop\InvestmentPromotion;

use App\Enums\OpenShop\InvestmentPromotion\SignOrderEnum;
use App\Enums\OpenShop\InvestmentPromotion\UserContractEnum;
use App\Http\Validate\AbstractBaseValidate;
use App\Models\OpenShop\InvestmentPromotion\OpenShopSignOrderModel;
use Illuminate\Support\MessageBag;
use Illuminate\Validation\Rule;
use Illuminate\Validation\ValidationException;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;

class OrderNewValidate extends AbstractBaseValidate
{
    function getRules(): array
    {
        // TODO: Implement getRules() method.
        return [];
    }

    function getAttributes(): array
    {
        // TODO: Implement getAttributes() method.
        return [
            "jflxdz"       => "甲方联系地址",//联系地址
            "yfxm"         => "乙方",//乙方
            "yfsfzh"       => "身份证号/社会统一信用代码",
            "yflxdz"       => "联系地址",
            "yflxfs"       => "联系方式",
            "qysj"         => "合约签订日期",
            "htbh"         => "合同编号",
            "yfsqdz"       => "授权区域",
            "jcsj"         => "解除日期",
            "jiri1"        => "签订日期之日？天",
            "jiri2"        => "签订日期之日？天",
            "jiri3"        => "签订日期之日？天",
            "tqfy1"        => "拖欠费用1",
            "tqfy2"        => "拖欠费用2",
            "tqfy3"        => "拖欠费用3",
            "tqfy4"        => "拖欠费用4",
            "ycxfy1"       => "一次性人民币",
            "ppsyf"        => "加盟/品牌使用费",
            "jssqf"        => "技术转让费",
            "lybzhj"       => "履约保证金",
            "txqsyf1"      => "特权使用费",
            "yunfuwu1"     => "系统云服务费用",
            "yfzhmc"       => "账户名称",
            "yfkhh"        => "开户行",
            "yfzh"         => "账号",
            "orderNo"      => "订单号",
            "reason"       => "作废原因",
            "contractType" => "合同类型",
            "lxdz"         => "甲方联系地址",
            "qdrq"         => "签约时间",
            "sqdz"         => "门店地址",
            "qcrq"         => "解约日期",
            "jmf"          => "加盟相关一次性费用",
            "ppsfy"        => "品牌使用费",
            "jsjyf"        => "技术授予费",
            "zxzdf"        => "咨询指导服务费",
            "txqsyf"       => "特许权使用费",
            "szhfy"        => "数字化运营系统云服务年费",
            "zxzfd1"       => "咨询指导费",
            "lybzj"        => "履约保证金"
        ];
    }

    function registerValidateRules(): void
    {
        // TODO: Implement registerValidateRules() method.
    }

    function getMessages(): array
    {
        // TODO: Implement getMessages() method.
        return [

        ];
    }

    /**
     * @param array $params
     * @return MessageBag|object
     * @throws ValidationException
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public static function invalidOrderTypeValidate(array $params)
    {
        return self::getInstance()->validate(
            ["orderNo"],
            $params,
            [
                "orderNo" => Rule::exists("open_shop_user_contract", "orderNo")->where("contractStatus", 0)
            ]
        );
    }

    /**
     * @param array $requestParams
     * @return MessageBag|object
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws ValidationException
     */
    public static function invalidOrderValidate(array $requestParams)
    {
        return self::getInstance()->validate(
            ["orderNo", "reason", "invalidateType", "reason", "contractType"],
            $requestParams,
            array_merge([
                'orderNo'        => [
                    'required',
                    Rule::exists('open_shop_sign_order')->where(function ($query) {
                        $query->where('status', "<>", SignOrderEnum::ORDER_STATUS_NO);
                    })
                ],
                "invalidateType" => self::intInValidateBuild(true, OpenShopSignOrderModel::INVALIDATE_TYPE_TEXT),
                "reason"         => "exclude_unless:invalidateType," . OpenShopSignOrderModel::INVALIDATE_TYPE_5 . "|required|string|min:1|max:500",
                "contractType"   => self::intInValidateBuild(true, UserContractEnum::CONTRACT_TYPE, [0]),
            ], self::getContractParamsRules($requestParams)),
            true,
            function ($params) {
                switch ($params["contractType"] ?? 0) {
                    case UserContractEnum::CONTRACT_TYPE_3:
                    case UserContractEnum::CONTRACT_TYPE_4:
                    case UserContractEnum::CONTRACT_TYPE_6:
                    case UserContractEnum::CONTRACT_TYPE_7:
                        if (empty($params["tqfy1"])) {
                            $params["tqfy1"] = "-";
                        }

                        if (empty($params["tqfy2"])) {
                            $params["tqfy2"] = "-";
                        }

                        if (empty($params["tqfy3"])) {
                            $params["tqfy3"] = "-";
                        }

                        if (empty($params["tqfy4"])) {
                            $params["tqfy4"] = "-";
                        }
                        break;
                }
                return $params;
            }
        );
    }

    public static function getContractParamsRules(array $params): array
    {
        $paramsRules = [];
        switch ($params["contractType"] ?? 0) {
            case UserContractEnum::CONTRACT_TYPE_3:
                $paramsRules = [
                    "yfxm"     => self::stringValidateBuild(true, 60, 1),//乙方
                    "yfsfzh"   => self::stringValidateBuild(true, 72, 1),
                    "yflxdz"   => self::stringValidateBuild(true, 72, 1),
                    "yflxfs"   => self::stringValidateBuild(true, 11, 11),
                    "qysj"     => self::stringValidateBuild(true, 12, 1),
                    "tqfy1"    => self::stringValidateBuild(false, 72, 1),
                    "tqfy2"    => self::stringValidateBuild(false, 72, 1),
                    "tqfy3"    => self::stringValidateBuild(false, 72, 1),
                    "tqfy4"    => self::stringValidateBuild(false, 72, 1),
                    "ycxfy1"   => self::stringValidateBuild(true, 72, 1),
                    "ppsyf"    => self::stringValidateBuild(true, 72, 1),
                    "jssqf"    => self::stringValidateBuild(true, 72, 1),
                    "lybzhj"   => self::stringValidateBuild(true, 72, 1),
                    "txqsyf1"  => self::stringValidateBuild(true, 72, 1),
                    "yunfuwu1" => self::stringValidateBuild(true, 72, 1),
                    "yfzhmc"   => self::stringValidateBuild(true, 72, 1),
                    "yfkhh"    => self::stringValidateBuild(true, 72, 1),
                    "yfzh"     => self::stringValidateBuild(true, 72, 1),
                    "yftxqsyf" => self::stringValidateBuild(true, 72, 1)
                ];
                break;
            case UserContractEnum::CONTRACT_TYPE_4:
                $paramsRules = [
                    "yfxm"     => self::stringValidateBuild(true, 60, 1),
                    "yfsfzh"   => self::stringValidateBuild(true, 72, 1),
                    "yflxdz"   => self::stringValidateBuild(true, 72, 1),
                    "yflxfs"   => self::stringValidateBuild(true, 11, 11),
                    "qysj"     => self::stringValidateBuild(true, 72, 1),
                    "tqfy1"    => self::stringValidateBuild(false, 72, 1),
                    "tqfy2"    => self::stringValidateBuild(false, 72, 1),
                    "tqfy3"    => self::stringValidateBuild(false, 72, 1),
                    "tqfy4"    => self::stringValidateBuild(false, 72, 1),
                    "ycxfy1"   => self::stringValidateBuild(true, 72, 1),
                    "ppsyf"    => self::stringValidateBuild(true, 72, 1),
                    "jssqf"    => self::stringValidateBuild(true, 72, 1),
                    "zxzfd1"   => self::stringValidateBuild(true, 72, 1),
                    "txqsyf1"  => self::stringValidateBuild(true, 72, 1),
                    "yunfuwu1" => self::stringValidateBuild(true, 72, 1),
                    "yfzhmc"   => self::stringValidateBuild(true, 72, 1),
                    "yfkhh"    => self::stringValidateBuild(true, 72, 1),
                    "yfzh"     => self::stringValidateBuild(true, 72, 1),
                    "lybzj"    => self::stringValidateBuild(true, 72, 1),
                    "yftxqsyf" => self::stringValidateBuild(true, 72, 1)
                ];
                break;
            case UserContractEnum::CONTRACT_TYPE_5:
                $paramsRules = [
                    "yfxm"     => self::stringValidateBuild(true, 60, 1),
                    "yfsfzh"   => self::stringValidateBuild(true, 72, 1),
                    "yflxdz"   => self::stringValidateBuild(true, 72, 1),
                    "yflxfs"   => self::stringValidateBuild(true, 11, 11),
                    "qdrq"     => self::stringValidateBuild(true, 72, 1),
                    "jmf"      => self::stringValidateBuild(true, 72, 1),
                    "ppsfy"    => self::stringValidateBuild(true, 72, 1),
                    "jsjyf"    => self::stringValidateBuild(true, 72, 1),
                    "zxzdf"    => self::stringValidateBuild(true, 72, 1),
                    "txqsyf"   => self::stringValidateBuild(true, 72, 1),
                    "szhfy"    => self::stringValidateBuild(true, 72, 1),
                    "yfzhmc"   => self::stringValidateBuild(true, 72, 1),
                    "yfkhh"    => self::stringValidateBuild(true, 72, 1),
                    "yfzh"     => self::stringValidateBuild(true, 72, 1),
                    "lybzj"    => self::stringValidateBuild(true, 72, 1),
                    "yftxqsyf" => self::stringValidateBuild(true, 72, 1)
                ];
                break;
            case UserContractEnum::CONTRACT_TYPE_6:
                $paramsRules = [
                    "yfxm"     => self::stringValidateBuild(true, 60, 1),//乙方
                    "yfsfzh"   => self::stringValidateBuild(true, 72, 1),
                    "yflxdz"   => self::stringValidateBuild(true, 72, 1),
                    "yflxfs"   => self::stringValidateBuild(true, 11, 11),
                    "qysj"     => self::stringValidateBuild(true, 12, 1),
                    "tqfy1"    => self::stringValidateBuild(false, 72, 1),
                    "tqfy2"    => self::stringValidateBuild(false, 72, 1),
                    "tqfy3"    => self::stringValidateBuild(false, 72, 1),
                    "tqfy4"    => self::stringValidateBuild(false, 72, 1),
                    "ycxfy1"   => self::stringValidateBuild(true, 72, 1),
                    "ppsyf"    => self::stringValidateBuild(true, 72, 1),
                    "jssqf"    => self::stringValidateBuild(true, 72, 1),
                    "lybzhj"   => self::stringValidateBuild(true, 72, 1),
                    "txqsyf1"  => self::stringValidateBuild(true, 72, 1),
                    "yunfuwu1" => self::stringValidateBuild(true, 72, 1),
                    "yfzhmc"   => self::stringValidateBuild(true, 72, 1),
                    "yfkhh"    => self::stringValidateBuild(true, 72, 1),
                    "yfzh"     => self::stringValidateBuild(true, 72, 1),
                    "yftxqsyf" => self::stringValidateBuild(true, 72, 1)
                ];
                break;
            case UserContractEnum::CONTRACT_TYPE_7:
                $paramsRules = [
                    "yfxm"     => self::stringValidateBuild(true, 60, 1),
                    "yfsfzh"   => self::stringValidateBuild(true, 72, 1),
                    "yflxdz"   => self::stringValidateBuild(true, 72, 1),
                    "yflxfs"   => self::stringValidateBuild(true, 11, 11),
                    "qdrq"     => self::stringValidateBuild(true, 72, 1),
                    "jmf"      => self::stringValidateBuild(true, 72, 1),
                    "ppsfy"    => self::stringValidateBuild(true, 72, 1),
                    "jsjyf"    => self::stringValidateBuild(true, 72, 1),
                    "zxzdf"    => self::stringValidateBuild(true, 72, 1),
                    "txqsyf"   => self::stringValidateBuild(true, 72, 1),
                    "szhfy"    => self::stringValidateBuild(true, 72, 1),
                    "yfzhmc"   => self::stringValidateBuild(true, 72, 1),
                    "yfkhh"    => self::stringValidateBuild(true, 72, 1),
                    "yfzh"     => self::stringValidateBuild(true, 72, 1),
                    "lybzj"    => self::stringValidateBuild(true, 72, 1),
                    "yftxqsyf" => self::stringValidateBuild(true, 72, 1),
                    "tqfy1"    => self::stringValidateBuild(false, 72, 1),
                    "tqfy2"    => self::stringValidateBuild(false, 72, 1),
                    "tqfy3"    => self::stringValidateBuild(false, 72, 1),
                    "tqfy4"    => self::stringValidateBuild(false, 72, 1),
                ];
                break;
        }
        return $paramsRules;
    }
}
